package io.renren.modules.myCourse.controller;

import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wuwenze.poi.ExcelKit;
import io.lettuce.core.dynamic.annotation.Param;
import io.renren.common.exception.RRException;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.myCourse.entity.*;
import io.renren.modules.myCourse.service.*;
import io.renren.modules.oss.cloud.OSSFactory;
import io.renren.modules.oss.entity.SysOssEntity;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author zlb
 * @email 316682546@qq.com
 * @date 2019-03-03 13:40:33
 */
@RestController
@RequestMapping("myCourse/myfile")
public class MyFileController extends AbstractController {
    @Autowired
    private MyFileService myFileService;
    @Autowired
    private ChooseTestService chooseTestService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("myCourse:myfile:list")
    public List list(@RequestParam Map<String, Object> params){
        List<MyFileEntity> menuList = myFileService.selectList(
                new EntityWrapper<MyFileEntity>()
                        .eq("user_id",getUserId().toString()));
        return  menuList;
    }


    /**
     * 列表
     */
    @RequestMapping("/getMyFilemenuTree")
    // @RequiresPermissions("myCourse:myfile:list")
    public List getMyFilemenuTree(@RequestParam Map<String, Object> params){
        List<MyFileEntity> menuList = myFileService.selectList(
                new EntityWrapper<MyFileEntity>()
                        .eq("user_id",getUserId().toString())
                        .eq("type","0")

        );
        return  menuList;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("myCourse:myfile:info")
    public R info(@PathVariable("id") Integer id){
        MyFileEntity myFile = myFileService.selectById(id);

        return R.ok().put("myFile", myFile);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("myCourse:myfile:save")
    public R save(@RequestBody MyFileEntity myFile){
        myFile.setUserId(getUserId().intValue());
        myFileService.insert(myFile);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("myCourse:myfile:update")
    public R update(@RequestBody MyFileEntity myFile){
        ValidatorUtils.validateEntity(myFile);
        myFileService.updateAllColumnById(myFile);//全部更新
        
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/updateParentId")
    // @RequiresPermissions("myCourse:myfile:update")
    public R updateParentId(@RequestBody MyFileEntity myFile){
      //  ValidatorUtils.validateEntity(myFile);
       // myFileService.updateAllColumnById(myFile);//全部更新
        myFile.setType(null);

        myFileService.updateById(myFile);
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("myCourse:myfile:delete")
    public R delete(@RequestBody Integer ids){
        List<MyFileEntity> menuList = myFileService.selectList(
                new EntityWrapper<MyFileEntity>()
                        .eq("parent_id",ids.toString()));
        if(menuList.size()==0) {
            myFileService.deleteBatchIds(Arrays.asList(ids));

            return R.ok();
        }else {
            return R.error(100,"请先删除子文件");
        }
    }

    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file,Integer parentId) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        if(parentId==0){
            throw new RRException("请给文件选择目录");
        }

        //上传文件
        String name=file.getOriginalFilename();
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
        MyFileEntity myFile=new MyFileEntity();
        //保存文件信息
        myFile.setType(1);
        myFile.setName(name);
        myFile.setParentId(parentId);
        myFile.setFilePath(url);
        myFile.setUserId(getUserId().intValue());
        myFileService.insert(myFile);


        return R.ok().put("returnUrl", "http://"+url);

    }


    @RequestMapping("/export")
    public void export(HttpServletResponse response,String courseId) {
            List<ChooseTestEntity> cse=new ArrayList<>();
            cse=chooseTestService.selectList(new EntityWrapper<ChooseTestEntity>()
            .eq("course_id",courseId)
            );
            //ExcelKit.$Export(ChooseTestEntity.class, response).toExcel(cse, "题");
            ExcelKit.$Export(ChooseTestEntity.class, response).downXlsx(cse, false);
    }



}
